package com.atguigu.gulimall.product.config;

import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * @Author : Jungle
 * @Description :
 */
@EnableConfigurationProperties(DataSourceProperties.class)
@Configuration
public class MySeataConfig {

    //@Autowired
    //private DataSourceProperties properties;
    //用这个注入
    //@EnableConfigurationProperties(DataSourceProperties.class)

    @Bean
    public DataSource  dataSource(DataSourceProperties properties){
        HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if (StringUtils.hasText(properties.getName())) {
            dataSource.setPoolName(properties.getName());
        }
        return new DataSourceProxy(dataSource);
    }
}
